<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* TODO: This file needs major cleanup & commenting.
*/

/**
* Ineo - Modules - Admin CP Categories
*
* @package		Ineo
* @subpackage   Modules
* @copyright	2006, Veriea
*/
class ineo_mod_acp_categories
{
	/**
	* Variable Setup
	*/
	var $ipsclass;
	var $core;
	
	/**
	* Initialize
	*/
	function module_init()
	{
		// ACP Module, Do nothing if not in ACP!
		if( !IN_ACP )
		{
			return false;
		}
	}
	
	/**
	* Default Function for this module:
	*/
	function module_default()
	{
		return $this->category_list();
	}
	
	function category_list()
	{
		// Page Title
		$this->ipsclass->admin->page_title = 'Category Manager';
		$this->ipsclass->admin->page_detail = 'From here you can manage your existing categories and add new ones.';
		
		// Get Category ID, if one is set!
		$this->ipsclass->input['id'] = empty($this->ipsclass->input['id']) ? 0 : intval($this->ipsclass->input['id']);
		
		// Get a list of categories with the given parent ID
		$cat_list = $this->get_cat_list($this->ipsclass->input['id']);
		
		// Display!
		$this->core->api->box( 'Categories' , "
				<table style='border-spacing: 1px' width='100%' cellspacing='0' cellpadding='0' style='margin: 0px'>
				<form action='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=order' method='POST'>
				<input type='hidden' name='id' value='{$this->ipsclass->input['id']}'>
					<tr>
						<td class='".$this->core->api->thead."' style='margin: 0; border: 0;'>Category</td>
						<td class='".$this->core->api->thead."' style='width: 10px; margin: 0; border: 0;'>Order</td>
						<td class='".$this->core->api->thead."' style='width: 10px; margin: 0; border: 0;'>Options</td>
					</tr>
					{$cat_list}
					<tr>
						<td colspan='3' class='".$this->core->api->tfoot."' style='text-align: right;'>
							<input type='submit' class='realbutton' value='Reorder Categories'></form> <form action='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=new_category&parent={$this->ipsclass->input['id']}' method='POST'><input type='submit' class='realbutton' value='New Category'></form>
						</td>
					</tr>
				</table>
			" );
	}
	
	/**
	* Reorder categories:
	*/
	function order()
	{
	  foreach ( $_POST as $key => $value )
	  {
		$value = intval( $value );
		
	    if ( $key != 'id' )
	    {
	      $this->ipsclass->DB->do_update( 'ineo_categories', array( 'category_order' => $value ), 'category_id = \''.intval(str_replace('order_dropdown_', '', $key)).'\'') ;
	    }
	  }
	  
	  $return = '<div class="tablefooter"><strong>Category order updated.</strong></div>';
	  
	  $this->core->api->box('Success!', $return);
	  
	  $this->core->categories->rebuild_cat_cache();
	  
	  $this->category_list();
	}
	
	/**
	* Get a list of categories from the database:
	*/
	function get_cat_list($id)
	{
		$this->ipsclass->DB->simple_construct( array(
											'select' => 'c.*, count( cnt.category_id ) as subcat_count',
											'from'   => array( 'ineo_categories' => 'c' ),
											'add_join' => array( 1 => array(
											                     'type' => 'left',
											                     'from' => array( 'ineo_categories' => 'cnt' ),
											                     'where' => 'cnt.category_parent = c.category_id' )),
											'where' => 'c.category_parent = ' . $id . ' group by c.category_id' ));
		$this->ipsclass->DB->simple_order( 'category_order, category_name ASC' );
		$queery = $this->ipsclass->DB->simple_exec();
		
		$catcount = $this->ipsclass->DB->get_num_rows();
		
		$list = '';
		
		while( $cat = $this->ipsclass->DB->fetch_row($queery) )
		{
		  $children = $cat['subcat_count'];
		  
			$cat_dropdown = $this->order_dropdown( $cat['category_id'], $cat['category_order'], $catcount );
			
			$sublink = "<a href='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&id={$cat['category_id']}'>";
			$children = ( $children > 0 ) ? "( <strong>{$sublink}{$children}</a></strong> children )" : "";
			
			$list .= "
					<tr>
						<td class='".$this->core->api->row2."' style='padding: 5px'><strong>{$sublink}{$cat['category_name']}</a></strong> {$children}<br />{$cat['category_desc']}</td>
						
						<td class='".$this->core->api->row2."' style='padding: 5px'>{$cat_dropdown}</td>
						
						<td class='".$this->core->api->row1."' style='padding: 5px; text-align: center; '>
						
						<img src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' class='ipd' alt='Options' id='cat-options-{$cat['category_id']}' />
						
						<script type='text/javascript'>
						menu_build_menu(
							'cat-options-{$cat['category_id']}',
							new Array(
									img_item + \"&nbsp; <a href='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=edit&cat_id={$cat['category_id']}'>Edit Category</a>\",
									img_item + \"&nbsp; <a href='{$this->ipsclass->base_url}&section=components&act=ineo&module=permissions&category={$cat['category_id']}'>Edit Permissions</a>\",
									img_item + \"&nbsp; <a href='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=delete&cat_id={$cat['category_id']}'>Delete Category</a>\",
									img_item + \"&nbsp; <a href='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=empty&cat_id={$cat['category_id']}'>Empty Category</a>\",
									img_item + \"&nbsp; {$sublink}View Children</a>\"
								)
							);
						 </script>
						
						</td>
					</tr>
					";
		}
		
		
		return $list;
	}
	
	/**
	* Make a dropdown select for reordering, giving only as many numbers as categories.
	*/
	function order_dropdown( $catid, $current_order, $total )
	{
		$return = '<select name="order_dropdown_'.$catid.'">';
		for ( $i=1; $i <= $total; $i++ )
		{
			$selected = ( $i == $current_order ) ? 'selected' : '' ;
			$return .= '<option value="'.$i.'" '.$selected.'>'.$i.'</option>';
		}
		$return .= '</select>';

		return $return;
	}
	
	/**
	* Delete a category:
	*/
	function delete()
	{
		$this->ipsclass->admin->page_title = 'Delete Category';
		$this->ipsclass->admin->page_detail = 'You can remove a category from here. Please note that the category must be empty and there must be no subcategories in this category.';

		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as count', 'from' => 'ineo_articles', 'where' => 'article_cat_id = '.intval($this->ipsclass->input['cat_id']) ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();

		if ( $data['count'] != 0 )
		{
			$opts .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;" colspan="2"><strong>Category is not empty!</strong><br /><i>You cannot delete a category that is not empty. Use the form below to move/delete articles in this category.</i></td></tr>';
			
			$opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Move Articles?</strong><br /><i>Choose the category to send all articles from this category to.</i></td>';
			
			$opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat_move&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST">'.$this->build_dropdown( null, true ).' <input type="submit" class="realbutton" value="Move Articles"></form></td></tr>';
			
			$opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Empty Category</strong><br /><i>All articles in the category will be deleted.</i></td>';
			
			$opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST"><input type="submit" class="realbutton" value="Delete Articles"></form></td></tr></table>';

			$this->core->api->box( 'Warning!', $opts );

			return true;
		}

		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as count', 'from' => 'ineo_categories', 'where' => 'category_parent = '.intval($this->ipsclass->input['cat_id']) ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();

		if ( $data['count'] != 0 )
		{
			$opts .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;" colspan="2"><strong>Category has subcategories!</strong><br /><i>You cannot delete a category that is not empty. Use the form below to move this category\'s subcats into another category.</i></td></tr>';
			
			$opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Move Subcats?</strong><br /><i>Choose the category to send all subcats from this category to.</i></td>';
			
			$opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=move_subcats&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST">'.$this->build_dropdown().' <input type="submit" class="realbutton" value="Move Subcategories"></form></td></tr></table>';
			
			$this->core->api->box( 'Warning!', $opts );

			return true;
		}
		
	  $do_it = '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;"><strong>Are you sure?</strong><br /><i>There is <b>NO</b> way to undo this action.</i></td><td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_delete_cat&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST"><input type="submit" class="realbutton" value="Delete Category"></form></td></tr></table>';
	  $this->core->api->box( 'Confirmation', $do_it );
	}
	
	function do_delete_cat()
	{
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as count', 'from' => 'ineo_articles', 'where' => 'article_cat_id = '.intval($this->ipsclass->input['cat_id']) ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		
		if ( $data['count'] != 0 )
		{
		  $opts .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;" colspan="2"><strong>Category is not empty!</strong><br /><i>You cannot delete a category that is not empty. Use the form below to move/delete articles in this category.</i></td></tr>';
	    $opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Move Articles?</strong><br /><i>Choose the category to send all articles from this category to.</i></td>';
	    $opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat_move&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST">'.$this->build_dropdown( null, true ).' <input type="submit" class="realbutton" value="Move Articles"></form></td></tr>';
	    $opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Empty Category</strong><br /><i>All articles in the category will be deleted.</i></td>';
	    $opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST"><input type="submit" class="realbutton" value="Delete Articles"></form></td></tr></table>';
		  
	    $this->core->api->box( 'Warning!', $opts );
		  
	    return true;
		}
		
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as count', 'from' => 'ineo_categories', 'where' => 'category_parent = '.intval($this->ipsclass->input['cat_id']) ) );
	  $this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		
		if ( $data['count'] != 0 )
		{
		  $opts .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;" colspan="2"><strong>Category has subcategories!</strong><br /><i>You cannot delete a category that is not empty. Use the form below to move this category\'s subcats into another category.</i></td></tr>';
	    $opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Move Subcats?</strong><br /><i>Choose the category to send all subcats from this category to.</i></td>';
	    $opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=move_subcats&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST">'.$this->build_dropdown().' <input type="submit" class="realbutton" value="Move Subcategories"></form></td></tr></table>';
	    $this->core->api->box( 'Warning!', $opts );
		  
	    return true;
		}
		
		$this->ipsclass->DB->simple_delete( 'ineo_categories' , 'category_id = \'' . intval($this->ipsclass->input['cat_id']) . '\'' );
		$this->ipsclass->DB->simple_exec();
		
		$this->core->libs['categories']->rebuild_cat_cache();
		
		$this->ipsclass->boink_it( $this->ipsclass->base_url . '&section=components&act=ineo&module=categories' );
	}
	
	function move_subcats()
	{
	  if ( $this->ipsclass->input['cat_id'] == $this->ipsclass->input['category_parent'] )
	  {
	    $return .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;" colspan="2"><strong>You cannot move subcats back into the same category.</strong><br /><i>That doesn\'t really accomplish much. ;) Use the form below to choose a different destination.</i></td></tr>';
	    $return .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Move subcats?</strong></td>';
	    $return .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=move_subcats&cat_id='.intval($this->ipsclass->input['cat_id']).'" method="POST">'.$this->build_dropdown().' <input type="submit" class="realbutton" value="Move Subcategories"></form></td></tr></table>';
	    $this->core->api->box( 'Warning!', $return );
	    return true;
	  }
	  
	  $this->ipsclass->DB->do_update( 'ineo_categories', array( 'category_parent' => $this->ipsclass->input['category_parent'] ), 'category_parent = '.intval($this->ipsclass->input['cat_id']) );
	  
	  $this->core->libs['categories']->rebuild_cat_cache();
	  
	  $this->ipsclass->boink_it( $this->ipsclass->base_url . '&section=components&act=ineo&module=categories' );
	}
	
	function empty_cat()
	{
		$this->ipsclass->admin->page_title = 'Empty Category';
		$this->ipsclass->admin->page_detail = 'You can either move articles into another category en-masse, or you can delete all articles (and the article\'s revisions, attachments, and watchlists) from here.';
		
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'ineo_categories', 'where' => 'category_id = '.intval($this->ipsclass->input['cat_id']) ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
	  
		$this->ipsclass->admin->page_title = 'Empty Category: '.$data['category_name'];
		$this->ipsclass->admin->page_detail = 'You can move all articles from the <b>'.$data['category_name'].'</b> category into another category, or delete all articles from <b>'.$data['category_name'].'</b> here.';
	  
	 	$opts .= '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;"><strong>Move Articles?</strong><br /><i>Choose the category to send all articles from this category to.</i></td>';
		$opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat_move&cat_id='.$this->ipsclass->input['cat_id'].'" method="POST">'.$this->build_dropdown( null, true ).' <input type="submit" class="realbutton" value="Move Articles"></form></td></tr>';
		$opts .= '<tr><td class="tablerow1" style="padding: 5px;"><strong>Empty Category</strong><br /><i>All articles in the category will be deleted.</i></td>';
		$opts .= '<td class="tablerow2" style="padding: 5px;"><form action="'.$this->ipsclass->base_url.'&section=components&act=ineo&module=categories&code=do_empty_cat&cat_id='.$this->ipsclass->input['cat_id'].'" method="POST"><input type="submit" class="realbutton" value="Delete Articles"></form></td></tr></table>';
	  
		$this->core->api->box( 'Empty Category: '.$data['category_name'], $opts );
	}
	
	function do_empty_cat_move()
	{
		$this->ipsclass->DB->do_update( 'ineo_articles', array( 'article_cat_id' => intval($this->ipsclass->input['category_parent']) ), 'article_cat_id = '.intval($this->ipsclass->input['cat_id']) );

		$this->core->libs['categories']->rebuild_cat_cache();

		$this->ipsclass->boink_it( $this->ipsclass->base_url . '&section=components&act=ineo&module=categories' );
	}

	function do_empty_cat()
	{
		$this->ipsclass->admin->page_title = 'Emptying Category';
		$this->ipsclass->admin->page_detail = 'This page displays the progress of emptying a category.';

		// Start output buffering, 'cause it rules.
		ob_start();

		$this->ipsclass->DB->simple_construct( array( 'select' => 'article_id', 'from' => 'ineo_articles', 'where' => 'article_cat_id = '.intval($this->ipsclass->input['cat_id']) ) );
		$this->ipsclass->DB->simple_limit( '50' );
		$this->ipsclass->DB->simple_exec();

		while ( $row = $this->ipsclass->DB->fetch_row() )
		{
			if ( isset($first) )
			{
				$id_string .= ','.$row['article_id'];
			}
			else 
			{
				$id_string .= $row['article_id'];
				$first = true;
			}
		}

		$count = $this->ipsclass->DB->get_num_rows();

		if ( $count != 0 )
		{
			// Delete Article, Article Content, Watchlist Items and Ratings.
			$this->ipsclass->DB->simple_delete( 'ineo_articles'    , 'article_id IN ( ' . $id_string . ' )' );
			$this->ipsclass->DB->simple_exec();
			$this->ipsclass->DB->simple_delete( 'ineo_content'     , 'content_article IN ( ' . $id_string . ' )' );
			$this->ipsclass->DB->simple_exec();
			$this->ipsclass->DB->simple_delete( 'ineo_watchlists'  , 'watch_id IN ( ' . $id_string . ' ) AND watch_type = 1' );
			$this->ipsclass->DB->simple_exec();
			$this->ipsclass->DB->simple_delete( 'ineo_rating'      , 'rate_artid IN ( ' . $id_string . ' )' );
			$this->ipsclass->DB->simple_exec();

			// Delete article's attachments.
			$this->ipsclass->DB->simple_construct( array( 'select' => '*','from' => 'ineo_attachments','where' => 'attach_article IN ( ' . $id_string . ' )' ) );
			$this->ipsclass->DB->simple_exec();

			while( $data = $this->ipsclass->DB->fetch_row() )
			{
				@unlink($data['attach_location']);
			}

			// Remove attachment entries in the DB for article.
			$this->ipsclass->DB->simple_delete( 'ineo_attachments', 'attach_article IN ( ' . $id_string . ' )' );
			$this->ipsclass->DB->simple_exec();
		}

		// Give 'em a number to see.
		$delcount = $this->ipsclass->input['del_count'];
		$delcount = ( empty($delcount) ) ? $count : $delcount + $count ;

		$return = '<table width="100%"><tr><td class="tablerow1" style="padding: 5px;"><strong>Articles deleted</strong></td><td class="tablerow2" style="padding: 5px;">'.$delcount.'</td></tr></table>';

		$this->core->api->box( 'Deletion progress', $return );

		if ( $count != '0' )
		{
			@header( "Refresh: 2;url=".$this->ipsclass->base_url."&section=components&act=ineo&module=categories&code=do_empty_cat&cat_id=".intval($this->ipsclass->input['cat_id'])."&del_count=".$delcount );
		}
		else
		{
			$this->core->libs['categories']->rebuild_cat_cache();
			@header( "Refresh: 2;url=".$this->ipsclass->base_url."&section=components&act=ineo&module=categories" );
		}

		ob_flush();
	}

	function edit()
	{
		$this->ipsclass->admin->page_title = 'Edit Category';
		$this->ipsclass->admin->page_detail = 'Edit settings for a category.';

		$this->ipsclass->DB->simple_construct( array('select' => '*','from'   => 'ineo_categories','where'  => 'category_id = \'' . intval($this->ipsclass->input['cat_id']) . '\'',));
		$this->ipsclass->DB->simple_exec();

		$cat = $this->ipsclass->DB->fetch_row();

		$parent_menu = $this->build_dropdown($cat['category_parent']);

		$this->core->api->box( 'Edit Category: ' . $cat['category_name'] , 
		"<form action='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=do_cat_edit&cat_id={$this->ipsclass->input['cat_id']}' method='post'>

		<table style='width: 100%' cellspacing='0'>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Category Name</b><br />The title of the category.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'><input class='forminput' type='text' name='category_name' value='{$cat['category_name']}' /></td>
		</tr>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Category Description</b><br />A brief description of the category.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'><input class='forminput' type='text' name='category_desc' value='{$cat['category_desc']}' /></td>
		</tr>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Parent Category</b><br />Select an existing category to make this a subcategory.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'>{$parent_menu}</td>
		<tr>
		<tr>
		<td colspan='2' class='".$this->core->api->tfoot."' style='text-align: center;'><input type='submit' value='Submit' class='realdarkbutton' /></td>
		</tr>
		</table>

		</form>
		" );
	}

	function new_category()
	{
		$this->ipsclass->admin->page_title = 'New Category';
		$this->ipsclass->admin->page_detail = 'Create a new category.';

		$parent_menu = $this->build_dropdown(intval($this->ipsclass->input['parent']));

		$this->core->api->box( 'New Category' ,
		"<form action='{$this->ipsclass->base_url}&section=components&act=ineo&module=categories&code=do_new_category' method='post'>

		<table style='width: 100%' cellspacing='0'>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Category Name</b><br />The title of the category.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'><input class='forminput' type='text' name='category_name'/></td>
		</tr>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Category Description</b><br />A brief description of the category.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'><input class='forminput' type='text' name='category_desc'/></td>
		</tr>
		<tr>
		<td class='".$this->core->api->row1."' style='padding: 5px'><b>Parent Category</b><br />Select an existing category to make this a subcategory.</td>
		<td class='".$this->core->api->row2."' style='padding: 5px'>{$parent_menu}</td>
		<tr>
		<td colspan='2' class='".$this->core->api->tfoot."' style='text-align: center;'><input type='submit' value='Submit' class='realdarkbutton' /></td>
		</tr>
		</table>

		</form>
		" );
	}

	function do_new_category()
	{
		$this->ipsclass->DB->do_insert( 'ineo_categories' , array( 'category_name' => $this->ipsclass->input['category_name'], 'category_desc' => $this->ipsclass->input['category_desc'], 'category_parent' => $this->ipsclass->input['category_parent'] ) );
		$this->core->libs['categories']->rebuild_cat_cache();
		$this->ipsclass->boink_it( $this->ipsclass->base_url . '&section=components&act=ineo&module=categories' );
	}

	function do_cat_edit()
	{
		$this->ipsclass->DB->do_update( 'ineo_categories' , array( 'category_name' => $this->ipsclass->input['category_name'] , 'category_desc' => $this->ipsclass->input['category_desc'], 'category_parent' => $this->ipsclass->input['category_parent'] ) , 'category_id = \'' . intval($this->ipsclass->input['cat_id']) . '\'' );
		$this->core->libs['categories']->rebuild_cat_cache();
		$this->ipsclass->boink_it( $this->ipsclass->base_url . '&section=components&act=ineo&module=categories' );
	}
	
	// Builds the dropdown for category listing.
	function build_dropdown($cid='', $no_root = false)
	{
	  $this->ipsclass->DB->simple_construct( array(
	                   'select' => 'category_id, category_name, category_parent',
	                   'from' => 'ineo_categories' ) );
    $this->ipsclass->DB->simple_exec();
    
    $this->depth_guide = '--';
	

    while( $row = $this->ipsclass->DB->fetch_row() )
    {
      $array[$row['category_parent']][$row['category_id']] = $row;
    }
    
    //echo "<pre>";print_r($array);die();
    
    $menu = '<select name="category_parent">';
	
	if( !$no_root )
	{
		$sel = ($this->ipsclass->input['parent'] == 0) ? ' selected="selected"' : '';

		$menu .= "<option value='0'{$sel}>Root Category</option>";
	}
	
    $menu .= $this->build_jump_internal( '0', $array, $cid );
    $menu .= '</select>';
    
    return $menu;
	}
	
	// Recursivityityity!!
  function build_jump_internal($root_id, $array, $selected_id, $jump_string="", $depth_guide="" )
  {
	
    if ( is_array( $array[ $root_id ] ) )
    {
      foreach( $array[ $root_id ] as $id => $cat_data )
      {
      $selected = "";
      if ( $selected_id == $cat_data['category_id'] )
      {
        $selected = ' selected="selected"';
      }
      
      $prefix = $depth_guide == '' ? '' : '&nbsp;&nbsp;&#0124;' ;
      
      $jump_string .= "<option value=\"{$cat_data['category_id']}\"".$selected.">".$prefix.$depth_guide." ".$cat_data['category_name']."</option>\n";
      $jump_string = $this->build_jump_internal( $cat_data['category_id'], $array, $selected_id, $jump_string, $depth_guide . $this->depth_guide );
      }
    }
	  return $jump_string;
  }
}

?>